package com.jyhg.core.mobile.effect
{
	import com.jyhg.core.mobile.base.JSprite;
	
	import starling.core.Starling;
	import starling.display.DisplayObject;
	import starling.display.Image;
	import starling.textures.RenderTexture;
	
	
	/**
	 * JScatterEffect
	 * @author: Jonny.Hong
	 * @date: 2014-2-10
	 */
	public class JScatterEffect extends JSprite
	{
		private var body:DisplayObject;		// 本体
		private var rowNum:int;					// 横向分裂数量
		private var columnNum:int;				// 纵向分裂数量
		private var scatters:Vector.<Image>;	//  散射集合
		
		public function JScatterEffect(display:DisplayObject,row:int,column:int)
		{
			rowNum = row;
			columnNum = column;
			scatters = new Vector.<Image>;
			body = display;
			super();
		}
		
		/**初始化*/		
		override public function init():void
		{
			super.init();
			
			var texWidth:int = body.width;
			var texHeight:int = body.width;
			// 复制纹理
			var rt:RenderTexture = new RenderTexture(texWidth,texHeight);
			rt.draw(body);
			body.visible = 0;
			
			var w:Number = 1/rowNum;
			var h:Number = 1/columnNum;
			var tw:int = w*texWidth;
			var th:int = w*texHeight;
			
			// 初始化小图快
			var img:Image;
			for (var i:int = 0; i < rowNum; i++)
			{
				for(var j:int = 0; j < columnNum; j++)
				{
					img = new Image(rt);
					img.width = tw;
					img.height = th;
					img.x = i*tw;
					img.y = j*th;
					
					img.setTexCoordsTo(0,i*w,j*h);
					img.setTexCoordsTo(1,(i+1)*w,j*h);
					img.setTexCoordsTo(2,i*w,(j+1)*h);
					img.setTexCoordsTo(3,(i+1)*w,(j+1)*h);
					
					addChild(img);
					scatters.push(img);
				}
			}
			
			draw();
		}
		
		/**绘制*/	
		public function draw():void
		{
			for each(var img:Image in scatters)
			{
				Starling.juggler.tween(img,0.5,{x: Math.random()*500,y:Math.random()*500});
			}
		}

	}
}